package com.eqdd.yiqidian.ui.select;

import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.view.View;

import com.bigkoo.pickerview.TimePickerView;
import com.eqdd.yiqidian.R;
import com.eqdd.yiqidian.SelectTimeWithLunarActivityCustom;
import com.eqdd.yiqidian.base.BaseActivity;
import com.eqdd.yiqidian.base.Config;
import com.eqdd.yiqidian.utils.DensityUtil;
import com.jakewharton.rxbinding.view.RxView;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit;

public class SelectTimeWithLunarActivity extends BaseActivity {
    private SelectTimeWithLunarActivityCustom dataBinding;
    private ArrayList<String> first = new ArrayList<>();
    private ArrayList<List<String>> second = new ArrayList<>();
    private com.eqdd.lunarView.TimePickerView pickerView;
    private TimePickerView nomalTimePicker;

    @Override
    public void initBinding() {
        dataBinding = DataBindingUtil.setContentView(this, R.layout.activity_select_time_with_lunar);
        initTopTitleBar(View.VISIBLE, "选择生日");
    }

    @Override
    public void initData() {
        RxView.clicks(dataBinding.tvNormal)
                .throttleFirst(1, TimeUnit.SECONDS)
                .filter(v-> !(dataBinding.llRootYang.getVisibility()==View.VISIBLE))
                .subscribe(v -> {
                    if (dataBinding.llRootYin.getVisibility()==View.VISIBLE) {
                        dataBinding.llRootYin.setVisibility(View.GONE);
                    }
                    dataBinding.llRootYang.setVisibility(View.VISIBLE);
                    dataBinding.tvNormal.setStrokeColor(getResources().getColor(R.color.main_color));
                    dataBinding.tvNormal.setStrokeWidth(DensityUtil.dip2px(this, 3));
                    dataBinding.tvLunar.setStrokeColor(getResources().getColor(R.color.white));
                });
        RxView.clicks(dataBinding.tvLunar)
                .throttleFirst(1, TimeUnit.SECONDS)
                .filter(v->!(dataBinding.llRootYin.getVisibility()==View.VISIBLE))
                .subscribe(v -> {
                    if (dataBinding.llRootYang.getVisibility()==View.VISIBLE) {
                        dataBinding.llRootYang.setVisibility(View.GONE);
                    }
                    dataBinding.llRootYin.setVisibility(View.VISIBLE);
                    dataBinding.tvLunar.setStrokeColor(getResources().getColor(R.color.main_color));
                    dataBinding.tvLunar.setStrokeWidth(DensityUtil.dip2px(this, 3));
                    dataBinding.tvNormal.setStrokeColor(getResources().getColor(R.color.white));
                });
    }

    @Override
    public void setView() {

        initCalendar();
    }

    private void initCalendar() {

        pickerView = new com.eqdd.lunarView.TimePickerView(this, com.eqdd.lunarView.TimePickerView.Type.YEAR_MONTH_DAY, dataBinding.llRootYin);
        pickerView.setLunarCalendar(true);
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        pickerView.setRange(1900, calendar.get(Calendar.YEAR));
        pickerView.setTime(new Date());
        pickerView.setOnTimeSelectListener(date -> {
                    Intent intent = new Intent();
                    Calendar instance = Calendar.getInstance();
                    instance.setTime(date);
                    intent.putExtra(Config.MONTH, instance.get(Calendar.MONTH) + 1);
                    intent.putExtra(Config.DAY, instance.get(Calendar.DAY_OF_MONTH));
                    intent.putExtra(Config.BIRTH_TYPE, Config.LUNAR);
                    setResult(Config.SUCCESS, intent);
                    finish();
                }
        );
//        pickerView = new OptionsPickerView.Builder(this, new OptionsPickerView.OnOptionsSelectListener() {
//            @Override
//            public void onOptionsSelect(int options1, int options2, int options3, View v) {
//                Intent intent = new Intent();
//                intent.putExtra(Config.MONTH,options1+1);
//                intent.putExtra(Config.DAY,options2+1);
//                intent.putExtra(Config.BIRTH_TYPE,Config.LUNAR);
//                setResult(Config.SUCCESS,intent);
//                finish();
//            }
//        }).setDecorView(dataBinding.llRoot)
//                .setLabels("月", "", "")
//                .isCenterLabel(true)
//                .build();
//        first = new ArrayList<>();
//        for (int i=0;i<12;i++) {
//            first.add(i+1+"");
//            ArrayList<String> temp = new ArrayList<>();
//            for (int j=0;j<30;j++) {
//                temp.add(j+1+"");
//            }
//            second.add(temp);
//        }
//        pickerView.setPicker(first, second);

        nomalTimePicker = new TimePickerView.Builder(this,(date, v) ->{
                Intent intent = new Intent();
                Calendar instance = Calendar.getInstance();
                instance.setTime(date);
                intent.putExtra(Config.MONTH, instance.get(Calendar.MONTH) + 1);
                intent.putExtra(Config.DAY, instance.get(Calendar.DAY_OF_MONTH));
                intent.putExtra(Config.BIRTH_TYPE, Config.NORMAL);
                setResult(Config.SUCCESS, intent);
                finish();
        }).setRange(1900, calendar.get(Calendar.YEAR))
                .setDate(Calendar.getInstance())
                .setDecorView(dataBinding.llRootYang)
                .setType(new boolean[]{true, true, true, false, false, false})
                .build();
        nomalTimePicker.show(false);
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {

        }
    }
}
